//
//  NDCitemFormatter.m
//  iMed
//
//  Created by Barry Press on 11/7/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "NDCitemFormatter.h"
#import "NDCitem.h"
#import "Meds.h"
#include "MyDocument.h"
#import "DataSet.h"


@implementation NDCitemFormatter

// ---------------------------------------------- stringForObjectValue ---------
- (NSString*)stringForObjectValue:(id)obj
{
	if ([obj isKindOfClass:[NDCitem class]]) {
	NDCitem* ndc = obj;
	return [NSString stringWithFormat:@"%@ %@", [ndc ndc], [ndc name]];
	}
	else if ([obj isKindOfClass:[NSString class]]) {
		return obj;
	}
	else {
		return nil;
	}

}

// ---------------------------------------------- getObjectValue:forString:errorDescription
- (BOOL)getObjectValue:(id *)obj 
			 forString:(NSString *)string 
	  errorDescription:(NSString **)error
{
	Meds* f = [CURRENTDATASET arrayMeds];
	NSAssert(f != nil, @"Can't find current data set");
	NDCitem* ndc = [f objectWithName:string];
	BOOL ret = NO;
	
	if (ndc != nil) { 
		*obj = ndc;
		ret = YES;
	}
	else {
		if (error != NULL) {
			*error = [NSString stringWithFormat:@"'%@' not found in formulary", string];
		}
	}
	return ret;
}

@end
